package com.chis.https;

import java.io.FileInputStream;
import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.ssl.SslHandler;

/**
 * 
 * @Description 
 * @Company zwx
 * @author wlj
 * @version 2019年12月19日
 */
public class HttpsServerInitializer extends ChannelInitializer<SocketChannel> {
//    @Override
//    protected void initChannel(SocketChannel ch) throws Exception {
//        ChannelPipeline ph = ch.pipeline();
//        //添加sslhandler
//        SSLContext sslContext = SSLContext.getInstance("TLSv1");
//        KeyStore ks = KeyStore.getInstance("JKS");
//        //加载keytool 生成的文件
//        char[] passArray = "123456".toCharArray(); //jks密码
//        FileInputStream inputStream = new FileInputStream("d:/test.jks");
//        ks.load(inputStream, passArray);
//        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
//        kmf.init(ks, passArray);
//        sslContext.init(kmf.getKeyManagers(), null, null);
//        inputStream.close();
//        SSLEngine sslEngine = sslContext.createSSLEngine();
//        sslEngine.setUseClientMode(false);
//        ch.pipeline().addLast(new SslHandler(sslEngine));
//        //处理http服务的关键handler
//        //一个组合的HttpRequestDecoder和HttpResponseEncoder使得服务器端HTTP实现更容易。
//        ph.addLast("codec", new HttpServerCodec());
//        ph.addLast("aggregator", new HttpObjectAggregator(10*1024*1024));
//        ph.addLast("handler", new HttpServerHandler());// 服务端逻辑处理类
//    }

//    @Override
//    protected void initChannel(SocketChannel ch) throws Exception {
//        ChannelPipeline ph = ch.pipeline();
//        //添加sslhandler
//        SSLContext sslContext = SSLContext.getInstance("TLSv1");
//        KeyStore ks = KeyStore.getInstance("pkcs12");
//        //加载keytool 生成的文件
//        char[] passArray = "123456".toCharArray(); //jks密码
//        FileInputStream inputStream = new FileInputStream("d:/test.pfx");
//        ks.load(inputStream, passArray);
//        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
//        kmf.init(ks, passArray);
//        sslContext.init(kmf.getKeyManagers(), null, null);
//        inputStream.close();
//        SSLEngine sslEngine = sslContext.createSSLEngine();
//        sslEngine.setUseClientMode(false);
//        ch.pipeline().addLast(new SslHandler(sslEngine));
//        //处理http服务的关键handler
//        //一个组合的HttpRequestDecoder和HttpResponseEncoder使得服务器端HTTP实现更容易。
//        ph.addLast("codec", new HttpServerCodec());
//        ph.addLast("aggregator", new HttpObjectAggregator(10*1024*1024));
//        ph.addLast("handler", new HttpServerHandler());// 服务端逻辑处理类
//    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline ph = ch.pipeline();
        //添加sslhandler
        SSLContext sslContext = SSLContext.getInstance("TLSv1");
        KeyStore ks = KeyStore.getInstance("pkcs12");
        //加载keytool 生成的文件
        char[] passArray = "nclient".toCharArray(); //jks密码
        FileInputStream inputStream = new FileInputStream("/home/ca/nclient.pfx");
        ks.load(inputStream, passArray);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ks, passArray);
        sslContext.init(kmf.getKeyManagers(), null, null);
        inputStream.close();
        SSLEngine sslEngine = sslContext.createSSLEngine();
        sslEngine.setUseClientMode(false);
        ch.pipeline().addLast(new SslHandler(sslEngine));
        //处理http服务的关键handler
        //一个组合的HttpRequestDecoder和HttpResponseEncoder使得服务器端HTTP实现更容易。
        ph.addLast("codec", new HttpServerCodec());
        ph.addLast("aggregator", new HttpObjectAggregator(10*1024*1024));
        ph.addLast("handler", new HttpServerHandler());// 服务端逻辑处理类
    }

}
